import store from "@/store";
import axios from "axios";


export function uploadFile(url, data, callback) {
    axios({
        url,
        headers: {
            "Content-Type": "multipart/form-data",
            "Authorization": store.state.token
        },
        data,
        method: "POST",
    }).then(res => {
        callback(res)
    }).catch(res=>{
        callback(res);
    })
};

export function downloadFile(url, data) {
    axios({
        url,
        // 设置响应类型为blob 
        responseType: 'blob',

        headers: {
            "Content-Type": "multipart/form-data",
            "Authorization": store.state.token
        },
        //参数以params或者headers 进行传递
        params: data,
        method: "POST",
    }).then(res => {
        //创建接收的数据
        let blob = new Blob([res.data]);
        // 创建指向blob的地址 
        let href = window.URL.createObjectURL(blob);
        //  创建下载链接 
        let a = document.createElement('a');
        // 下载链接添加地址 
        a.href = href;
        // 下载链接添加文件名
        a.download = res.headers.filename;
        // 触发链接点击事件
        a.click();
        // 移除释放地址
        window.URL.revokeObjectURL(href);
    })
}
export async function getImg(url) {
    let href = await axios({
        url,
        // 设置响应类型为blob 
        responseType: 'blob',
        headers: {
            "Content-Type": "multipart/form-data",
            "Authorization": store.state.token
        },
        method: "GET",
    }).then(res => {
        //创建接收的数据
        let blob = new Blob([res.data]);
        // 创建指向blob的地址 
        let href = window.URL.createObjectURL(blob);
        return href;
    });
    return href;
}